import os
import re

def search_files_in_folder(folder_path, regex_pattern):
    # 遍历文件夹中的所有文件
    for filename in os.listdir(folder_path):
        # 检查文件是否是 .txt 文件
        if filename.endswith(".txt"):
            file_path = os.path.join(folder_path, filename)

            # 打开文件并逐行查找匹配正则表达式的行
            with open(file_path, 'r') as file:
                for line_number, line in enumerate(file, 1):
                    if re.search(regex_pattern, line):
                        print(f'{file_path} - Line {line_number}: {line.strip()}')


# 获取用户输入的文件夹路径和正则表达式
folder_path = input("Enter the folder path: ")
regex_pattern = input("Enter the regular expression: ")

# 调用函数进行搜索并打印结果
search_files_in_folder(folder_path, regex_pattern)
